package com.rg.inf.action;

import com.rd.sys.dto.client.sync.MsgBody;
import com.rd.sys.dto.client.sync.SchemeSyncMsgBody;
import com.rd.sys.dto.commmon.base.EyeException;
import com.rd.sys.dto.commmon.base.EyeResult;
import com.rd.sys.dto.commmon.enums.SyncEnum;
import com.rd.sys.dto.sys.user.UserInfoDto;
import com.rd.sys.dto.sys.user.UserSubscriptionDto;
import com.rd.sys.service.analysis.scheme.SchemeService;

public class SchemeSyncAction extends BaseExecuteAction {
    private SchemeService schemeService = (SchemeService) getService("schemeServiceImpl");

    @Override
    public EyeResult execute(UserInfoDto userInfo, MsgBody msgBody, Object... objs) throws EyeException, Exception {
        SchemeSyncMsgBody body = (SchemeSyncMsgBody) msgBody;

        /** 1.检查项目存在性 */
        super.checkProjectNotExist(userInfo.getUsercode(), body.getProjectCode());

        /** 2.检查方案存在性 */
        boolean isExist = super.checkSchemeExist(userInfo.getUsercode(), body.getProjectCode(), body.getSchemeCode(),
                false);

        /** 3.检查订购关系是否正常 */
        UserSubscriptionDto sub = super.checkSubscription(userInfo.getUserno());

        /** 4.赋值操作代码 */
        String opercode = body.getOpercode();
        if (isExist) {
            if (SyncEnum.Opercode.ADD.getCode().equals(opercode)) { // 当方案路径存在,且操作代码为新增时,将默认为修改操作
                opercode = SyncEnum.Opercode.EDIT.getCode();
            }
        }

        /** 5.新增方案,检查订购套餐是否正常 */
        if (!isExist) { // 当前通过isExist来检查
            super.checkPackage(sub, false, true, false);
        }

        /** 6.同步方案信息 */
        schemeService.syncScheme(userInfo.getUserno(), userInfo.getUsercode(), body.getProjectCode(),
                super.transScheme(body), opercode);

        return new EyeResult(EyeResult.ResultDefine.COMMON_SUCCESS);
    }
}
